package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.MarketCategory;
import com.cskaoyan.bean.chenfeng.vo.CategoryL1Vo;
import com.cskaoyan.bean.chenfeng.vo.CategorySimpleL1Vo;
import com.cskaoyan.bean.param.CommonData;
import com.cskaoyan.service.CategoryService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * 商品类目部分
 * [后台系统 → 商场管理 → 商品类目]
 *
 * @author Chen_Feng
 * @since 2022/06/26 20:29
 */
@RestController
@RequestMapping("admin/category")
public class CategoryController {

    @Autowired
    CategoryService categoryService;

    /**
     * 查询
     * 获取全部商品类目，实际上是获得L1级别的商品类目和其下的子类
     *
     * @return com.cskaoyan.bean.BaseRespVo<CategoryL1Vo>
     * @author Chen_Feng
     * @since 2022/06/26 20:38
     */
    @RequiresPermissions(value = {"admin:category:list", "*"}, logical = Logical.OR)
    @RequestMapping("list")
    public BaseRespVo<CategoryL1Vo> list() {

        // 执行Service层业务
        CommonData<CategoryL1Vo> data = null;
        try {
            data = categoryService.getL1CategoryWithSub();
            return BaseRespVo.ok(data);
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.invalidData("数据获取失败");
        }
    }

    /**
     * 查询
     * 获取l1级别商品类目
     *
     * @return com.cskaoyan.bean.BaseRespVo<com.cskaoyan.bean.chenfeng.vo.CategorySimpleL1Vo>
     * @author Chen_Feng
     * @since 2022/06/26 21:41
     */
    @RequiresPermissions(value = {"admin:category:list", "*"}, logical = Logical.OR)
    @RequestMapping("l1")
    public BaseRespVo<CategorySimpleL1Vo> l1() {

        // 执行Service层业务
        CommonData<CategorySimpleL1Vo> data = null;
        try {
            data = categoryService.getL1Category();
            return BaseRespVo.ok(data);
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.invalidData("数据获取失败");
        }
    }

    /**
     * 增加
     * 新增商品类目，商品名称在同一父类同一级别中不可重复
     *
     * @param marketCategory 装有商品类目信息的对象
     * @return com.cskaoyan.bean.BaseRespVo<com.cskaoyan.bean.MarketCategory>
     * @author Chen_Feng
     * @since 2022/06/26 21:48
     */
    @RequiresPermissions(value = {"admin:category:create", "*"}, logical = Logical.OR)
    @RequestMapping("create")
    public BaseRespVo<MarketCategory> create(@RequestBody MarketCategory marketCategory) {

        // 父类目校验，非一级类目其pid不允许为0
        int pid = marketCategory.getPid();
        if (!Objects.equals(marketCategory.getLevel(), "L1") && pid == 0) {
            return BaseRespVo.invalidParameter("请更换所属父类目");
        }

        // 类目名称校验
        String name = marketCategory.getName();
        String trimmedName = null;
        if (name == null || (trimmedName = name.trim()).isEmpty()) {
            return BaseRespVo.invalidParameter("类目名称不能为空或纯空格");
        }
        if (categoryService.isCategoryExist(marketCategory)) {
            return BaseRespVo.invalidParameter("该类目名称已存在");
        }

        // 执行Service业务
        try {
            MarketCategory data = categoryService.createCategory(marketCategory);
            return BaseRespVo.ok(data);
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.invalidData("信息保存失败");
        }
    }

    /**
     * 修改
     * 对商品类目信息进行修改
     *
     * @param marketCategory
     * @return com.cskaoyan.bean.BaseRespVo
     * @author Chen_Feng
     * @since 2022/06/26 23:17
     */
    @RequiresPermissions(value = {"admin:category:update", "*"}, logical = Logical.OR)
    @RequestMapping("update")
    public BaseRespVo update(@RequestBody MarketCategory marketCategory) {

        // 父类目校验
        Integer pid = marketCategory.getPid();
        if (pid == null) {
            pid = 0;
            marketCategory.setPid(pid);
        }
        if (!Objects.equals(marketCategory.getLevel(), "L1") && pid == 0) {
            return BaseRespVo.invalidParameter("请更换所属父类目");
        }

        // 类目名称校验
        String name = marketCategory.getName();
        String trimmedName = null;
        if (name == null || (trimmedName = name.trim()).isEmpty()) {
            return BaseRespVo.invalidParameter("类目名称不能为空或纯空格");
        }
        if (categoryService.isCategoryExist(marketCategory)) {
            return BaseRespVo.invalidParameter("该类目名称已存在");
        }

        // 执行Service业务
        try {
            boolean flag = categoryService.updateMarketCategory(marketCategory);
            if (flag) {
                return BaseRespVo.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return BaseRespVo.invalidData("信息保存失败");
    }

    /**
     * 删除
     * 删除对应的类目以及其下子类，实则是将对应的记录的删除状态
     * 更改为true，即不可见
     *
     * @param marketCategory 封装了要删除类目的信息的对象
     * @return com.cskaoyan.bean.BaseRespVo
     * @author Chen_Feng
     * @since 2022/06/26 23:29
     */
    @RequiresPermissions(value = {"admin:category:delete", "*"}, logical = Logical.OR)
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody MarketCategory marketCategory) {

        // 执行Service层业务
        try {
            boolean flag = categoryService.deleteCategoryWithSub(marketCategory);
            if (flag) {
                return BaseRespVo.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return BaseRespVo.invalidData("删除失败");
    }
}
